﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace UnitTesting.Fixturing
{
	class LooseVerboseConstructorProvider : IConstructorProvider
	{
		readonly IConstructors _constructors;

		internal LooseVerboseConstructorProvider(IConstructors constructors)
		{
			_constructors = constructors;
		}

		public ConstructorInfo GetConstructorFor(Type type)
		{
			Ensure.IsNotNull(() => type);
			return GetVerboseConstructorFor(type);
		}

		ConstructorInfo GetVerboseConstructorFor(Type type)
		{
			return GetConstructors(type)
				.OrderByDescending(x => x.GetParameters().Length)
				.FirstOrDefault();
		}

		IEnumerable<ConstructorInfo> GetConstructors(Type type)
		{
			return _constructors.GetFor(type);
		}
	}
}